package com.peng;

import com.design_pattern.stragery.SuperFireStragery;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TankFrame extends Frame {

    public static final int GAME_WIDTH = Integer.parseInt(PropertiesManager.getValue("GAME_WIDTH"));
    public static final int GAME_HEIGHT = Integer.parseInt(PropertiesManager.getValue("GAME_HEIGHT"));
    private Image image = null;


    public TankFrame() {

        setResizable(false);
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        addKeyListener(new MoveListener());

    }


    @Override
    public void paint(Graphics g) {
        GameModel.getInstance().paint(g);
    }

    /**
     * 双缓冲解决闪屏问题 ，双缓冲解决闪烁问题（利用一次性加载内存中的图片达到效果）
     *
     * @param g
     */
    @Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics graphics = image.getGraphics();
        Color color = graphics.getColor();
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        graphics.setColor(color);
        paint(graphics);
        g.drawImage(image, 0, 0, null);

    }


    class MoveListener extends KeyAdapter {
        /**
         * 记录哪个键被按下了
         */
        boolean bL = false;
        boolean bT = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bT = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
            }
            locateDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {

            int keyCode = e.getKeyCode();

            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bT = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;

                case KeyEvent.VK_CONTROL:
//                            GameModel.getInstance().getMainTank().toNoticeFire();
                    GameModel.getInstance().getMainTank().fire(SuperFireStragery.getInstance());
                    break;

                case KeyEvent.VK_S:
                    GameModel.getInstance().save();
                    break;
                case KeyEvent.VK_L:
                    GameModel.getInstance().load();
                    break;
            }
            locateDir();

        }

        /**
         * 定位坦克的方向
         */
        void locateDir() {
            if (!bL && !bT && !bR && !bD) {
                GameModel.getInstance().getMainTank().setMoving(false);
            } else {
                GameModel.getInstance().getMainTank().setMoving(true);
                if (bL && !bT && !bR && !bD) GameModel.getInstance().getMainTank().setDir(Dir.L);
                else if (bL && bT && !bR && !bD) GameModel.getInstance().getMainTank().setDir(Dir.LU);
                else if (!bL && bT && !bR && !bD) GameModel.getInstance().getMainTank().setDir(Dir.U);
                else if (!bL && bT && bR && !bD) GameModel.getInstance().getMainTank().setDir(Dir.RU);
                else if (!bL && !bT && bR && !bD) GameModel.getInstance().getMainTank().setDir(Dir.R);
                else if (!bL && !bT && bR && bD) GameModel.getInstance().getMainTank().setDir(Dir.RD);
                else if (!bL && !bT && !bR && bD) GameModel.getInstance().getMainTank().setDir(Dir.D);
                else if (bL && !bT && !bR && bD) GameModel.getInstance().getMainTank().setDir(Dir.LD);

            }
        }


    }
}
